package com.crm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.base.ResponseResult;
import com.crm.domain.dto.QueryActivity;
import com.crm.domain.entity.Activity;
import com.crm.service.ActivityService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author Xanny
 */

@RestController
@RequestMapping("/clues/activity")
@RequiredArgsConstructor
public class ActivityController {

    private final ActivityService activityService;

    /**
     * 分页查询
     * @param queryActivity
     * @return
     */
    @GetMapping("/list")
    public ResponseResult queryAllActivity(QueryActivity queryActivity) {
        Page<Activity> page = activityService.queryAll(queryActivity);
        return ResponseResult.success()
                .put("rows", page.getRecords())
                .put("total", page.getTotal());
    }

    /**
     * 新增
     * 需要完成编号的随机生成
     * @param activity
     * @return
     */
    @PostMapping
    public ResponseResult addActivity(@RequestBody Activity activity) {
        activityService.addActivity(activity);
        return ResponseResult.success();
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseResult getActivityById(@PathVariable Long id) {
        return ResponseResult.success(activityService.getById(id));
    }

    /**
     * 根据Id更新
     * @param activity
     * @return
     */
    @PutMapping
    public ResponseResult updateActivityById(@RequestBody Activity activity) {
        activityService.updateById(activity);
        return ResponseResult.success();
    }

    /**
     * 根据Id删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseResult deleteActivityById(@PathVariable Long id) {
        activityService.removeById(id);
        return ResponseResult.success();
    }
}
